package com.yuyou.fn.educational.service.impl;

import com.github.wens.framework.service.impl.SuperServiceImpl;
import com.github.wens.mybatisplus.examples.Example;
import com.github.wens.mybatisplus.plugins.Page;
import com.yuyou.fn.common.exception.BusException;
import com.yuyou.fn.educational.entity.StudentGuardian;
import com.yuyou.fn.educational.service.IBaseGradeService;
import com.yuyou.fn.educational.service.IStudentGuardianService;
import com.yuyou.fn.common.util.BeanUtils;
import com.yuyou.fn.common.util.TimeUtils;
import com.yuyou.fn.educational.vo.StudentDetailVo;
import com.yuyou.fn.educational.vo.StudentSaveOrUpdateVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.annotation.Transient;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import com.yuyou.fn.educational.mapper.StudentMapper;
import com.yuyou.fn.educational.entity.Student;
import com.yuyou.fn.educational.service.IStudentService;

import java.util.Calendar;
import javax.annotation.Resource;

import java.util.List;

/**
 * @author Terry
 *         Student 表数据服务层接口实现类
 */
@Service
public class StudentServiceImpl extends SuperServiceImpl<StudentMapper, Student, Long> implements IStudentService {


    @Resource
    private RedisTemplate redisTemplate;

    @Resource
    private IStudentGuardianService studentGuardianService;

    @Resource
    private IBaseGradeService baseGradeService;

    /**
     * 查询符合条件的学生信息
     *
     * @return
     */
    public Page<Student> findStudentByKeyword(String keyword, int pageNo, int pageSize) {
        Example<Student> studentExample = Student.createExample();
        studentExample.orderBy("updateTime").desc();
        keyword = "%" + keyword + "%" ;
        studentExample.or().andLike("name" , keyword );
        studentExample.or().andLike("phoneNo" , keyword );
        studentExample.or().andLike("studentNo" , keyword );
        return findPageByExample(studentExample, pageNo, pageSize);
    }

    /**
     * 增加或更新学生信息
     *
     * @param studentSaveOrUpdateVo
     */
    @Transient
    public Long saveOrUpdateStudent(StudentSaveOrUpdateVo studentSaveOrUpdateVo) {
        Student student = new Student();
        BeanUtils.copyProperties(student, studentSaveOrUpdateVo);

        if (studentSaveOrUpdateVo.getGradeId() != null) {
            student.setGradeName(baseGradeService.findGradeNameById(student.getGradeId()));
        }

        checkStudent(student);
        if (student.getStudentId() == null) {
            student.setStudentNo(newStudentNo());
            student.setCreateTime(TimeUtils.now());
            student.setUpdateTime(TimeUtils.now());
            insertSelective(student);
        } else {
            student.setUpdateTime(TimeUtils.now());
            updateSelectiveById(student);
        }

        studentGuardianService.saveOrUpdateGuardian(student.getStudentId(), studentSaveOrUpdateVo.getGuardianList());

        return student.getStudentId();
    }

    private void checkStudent(Student student) {
        Example<Student> example = Student.createExample();
        example.createCriteria().andEqualTo("phoneNo",student.getPhoneNo()).andEqualTo("name",student.getName() );

        Student one = findOneByExample(example);

        if(one != null && ( student.getStudentId() == null || !student.getStudentId().equals(one.getStudentId()) ) ){
            throw new BusException("学生信息已经存在【姓名+手机号码】");
        }
    }


    @Override
    public StudentDetailVo findStudentDetailByStudentId(Long studentId) {
        Student student = findById(studentId);
        List<StudentGuardian> guardianList = studentGuardianService.findGuardianByStudentId(studentId);
        StudentDetailVo studentDetailVo = new StudentDetailVo();
        studentDetailVo.setStudent(student);
        studentDetailVo.setGuardianList(guardianList);
        return studentDetailVo;
    }

    /**
     * 生成学生编号
     *
     * @return
     */
    private String newStudentNo() {
        Calendar now = Calendar.getInstance();
        int year = now.get(Calendar.YEAR);
        Long no = redisTemplate.opsForValue().increment("student_no", 1);
        return year + StringUtils.leftPad(String.valueOf(no), 6, "0");
    }

    @Override
    public Student findByNameAndPhone(Student student) {
        Example<Student> example = Student.createExample();
        example.createCriteria().andEqualTo("phoneNo",student.getPhoneNo()).andEqualTo("name",student.getName() );

        Student one = findOneByExample(example);

        return one;
    }
}